import requests
import json

class Talk:
    #替换为自己的微信对话开放平台appid和token
    WEIXIN_TALK_APPID = ''
    WEIXIN_TALK_TOKEN = ''
    DEVICE = 'test'
    SIGN_URL = 'https://openai.weixin.qq.com/openapi/sign/' + WEIXIN_TALK_TOKEN
    TALK_URI = 'https://openai.weixin.qq.com/openapi/aibot/' + WEIXIN_TALK_TOKEN

    def get_signature(self):
        r = requests.post(self.SIGN_URL, data={'userid': self.DEVICE})
        if r.status_code == 200:
            return r.json()['signature']
        else:
            return 'false'

    def get_talk(self,message):
        s = self.get_signature()
        if s != 'false':
            try:
                r = requests.post(self.TALK_URI, data={'signature': s, 'query': message})
                res = r.json()['answer']
            except BaseException:
                return "不好意思没有理解到你的话哦"
            else:
                return res
        else:
            return False
